package com.yanfan.energy.controller;

import com.yanfan.attach.domain.DeviceDetail;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.EconomicalBody;
import com.yanfan.energy.domain.statist.DateStatistics;
import com.yanfan.energy.domain.statist.EnergyMaxStatistics;
import com.yanfan.energy.domain.statist.WorkDateStatistics;
import com.yanfan.energy.service.WorkshopEnergyService;
import com.yanfan.energy.service.WorkshopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @author caijinkang
 * @date 2022年09月30日 9:09
 */
@RestController
@RequestMapping("/energy")
@Api(tags = "车间能耗分析")
public class WorkshopEnergyController {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final Logger log = LoggerFactory.getLogger(WorkshopEnergyController.class);
    @Autowired
    private WorkshopEnergyService workshopEnergyService;

    @Autowired
    private WorkshopService workshopService;


    @GetMapping("/workshop-energy/consumed-water")
    @ApiOperation("耗水统计")
    public AjaxResult findConsumeWater(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findConsumeWater(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/gas-consumption")
    @ApiOperation("耗气统计")
    public AjaxResult findGasConsumption(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findGasConsumption(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/total-electricity")
    @ApiOperation("时段用电总量")
    public AjaxResult findTotalElectricity(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findTotalElectricity(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/air-conditioner")
    @ApiOperation("空调热能")
    public AjaxResult findAirConditioner(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findAirConditioner(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/energy-statistics")
    @ApiOperation("能耗排名")
    public AjaxResult findEnergyStatistics(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findEnergyStatistics(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @PostMapping("/workshop-energy/workshop-comparison")
    @ApiOperation("车间能耗对比")
    public AjaxResult findWorkshopComparison(@ApiParam(name = "workDateStatistics", value = "车间能耗对象") @RequestBody WorkDateStatistics workDateStatistics) {
        EnergyMaxStatistics statistics = workshopEnergyService.findWorkshopComparison(workDateStatistics.getWorkshopStatistics(), LocalDate.parse(workDateStatistics.getBegin(), formatter), LocalDate.parse(workDateStatistics.getEnd(), formatter), workDateStatistics.getUnit());

        return AjaxResult.success("查询成功", statistics);
    }

    @PostMapping("/workshop-energy/finWorkshopComparisonRevise")
    @ApiOperation("车间能耗对比")
    public AjaxResult finWorkshopComparisonRevise(@ApiParam(name = "workDateStatistics", value = "车间能耗对象") @RequestBody WorkDateStatistics workDateStatistics) {
        EnergyMaxStatistics statistics = workshopEnergyService.finWorkshopComparisonRevise(workDateStatistics.getWorkshopStatistics(), LocalDate.parse(workDateStatistics.getBegin(), formatter), LocalDate.parse(workDateStatistics.getEnd(), formatter), workDateStatistics.getUnit());

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop")
    @ApiOperation("查看所有车间")
    public AjaxResult findWorkshop() {
        List<DateStatistics> statistics = workshopService.findWorkshop();
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/classify")
    @ApiOperation("能耗分类")
    public AjaxResult findClassify() {

        List<DateStatistics> statistics = workshopEnergyService.findClassify();

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/typological")
    @ApiOperation("能耗类型")
    public AjaxResult findTypological(@ApiParam(name = "cId", value = "能耗类型id") @RequestParam Integer cId) {

        List<DateStatistics> statistics = workshopEnergyService.findTypological(cId);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/typological-type")
    @ApiOperation("能耗类型属性")
    public AjaxResult findTypologicalType(@ApiParam(name = "cId", value = "能耗属性id") @RequestParam int cId) {

        List<DateStatistics> statistics = workshopEnergyService.findTypologicalType(cId);

        return AjaxResult.success("查询成功", statistics);
    }


    @GetMapping("/findPurchasing")
    @ApiOperation("查看能源采购类")
    public AjaxResult findPurchasing() {
        return workshopEnergyService.findPurchasing();
    }

    @GetMapping("/findEnergyTypeId")
    @ApiOperation("能源介质类型下拉框")
    public AjaxResult findEnergyTypeId() {
        return workshopEnergyService.findEnergyTypeId();
    }

    @GetMapping("/purchaseByRange")
    @ApiOperation("查询能源采购的本年的数据")
    public AjaxResult purchaseByRange(@ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam(required = false) Integer unit) {
        return workshopEnergyService.purchaseByRange(range, workshopId, floorId, buildingId, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);
    }

    @GetMapping("/purchaseByMonth")
    @ApiOperation("查询能源水、电、气、采购的本月的数据")
    public AjaxResult purchaseByMonth(String range) {
        return workshopEnergyService.purchaseByMonth(range);
    }

    @GetMapping("/findDataOverview")
    @ApiOperation("查询省钱管理的数据概览")
    public AjaxResult findDataOverview() {
        return workshopEnergyService.findDataOverview();
    }

    @GetMapping("/findDataTrend")
    @ApiOperation("查询省钱管理的数据趋势")
    public AjaxResult findDataTrend(@ApiParam(name = "range", value = "日期格式") @RequestParam String range) {
        return workshopEnergyService.findDataTrend(range);
    }

    @GetMapping("/findSavingsRatio")
    @ApiOperation("查询本月节省占比")
    public AjaxResult findSavingsRatio() {
        return workshopEnergyService.findSavingsRatio();
    }

    @PostMapping("/findSaveMoneyData")
    @ApiOperation("查询省钱数据")
    public AjaxResult findSaveMoneyData(@ApiParam(name = "economicalBody", value = "省钱数据列表对象") @RequestBody EconomicalBody economicalBody) {
        return workshopEnergyService.findSaveMoneyData(economicalBody);
    }

    @GetMapping("/findLowEbbProportion")
    @ApiOperation("查询本月谷电占比")
    public AjaxResult findLowEbbProportion() {
        return workshopEnergyService.findLowEbbProportion();
    }

    @GetMapping("/findSaveMoneyMonthlyRanking")
    @ApiOperation("查询省钱月份排名")
    public AjaxResult findSaveMoneyMonthlyRanking() {
        return workshopEnergyService.findSaveMoneyMonthlyRanking();
    }

    @GetMapping("/findAllEnergyByDay")
    @ApiOperation("查询本日的总费用、水费、电费、气费、本日采购")
    public AjaxResult findAllEnergyByDay() {
        return workshopEnergyService.findAllEnergyByDay();
    }


    /**
     * 能耗统计-采购统计
     *
     * @return
     */
    @ApiOperation("新能耗统计-采购统计 折线图")
    @GetMapping("/purchaseStatistics")
    public AjaxResult purchaseStatistics(@ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "deviceId", value = "设备ID") @RequestParam(required = false) Long deviceId, @ApiParam(name = "deviceName", value = "设备名称") @RequestParam(required = false) String deviceName, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {

        Map<String, Map<String, Double>> statistics = workshopEnergyService.purchaseStatistics(buildingId, floorId, workshopId, deviceId, deviceName, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);
        return AjaxResult.success("查询成功", statistics);
    }

    /**
     * 能耗统计-采购统计 饼图
     *
     * @return
     */
    @ApiOperation("新能耗统计-采购统计 （饼图）")
    @GetMapping("/purchaseStatisticsPie")
    public AjaxResult purchaseStatisticsPie(@ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "deviceId", value = "设备ID") @RequestParam(required = false) Long deviceId, @ApiParam(name = "deviceName", value = "设备名称") @RequestParam(required = false) String deviceName, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {

        Map<String, Double> statistics = workshopEnergyService.purchaseStatisticsPie(buildingId, floorId, workshopId, deviceId, deviceName, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);
        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/workshop-energy/energyCostStatistics")
    @ApiOperation("新能耗统计-能耗费用")
    public AjaxResult energyCostStatistics(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "deviceId", value = "设备id") @RequestParam(required = false) Integer deviceId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findEnergyCostStatistics(workshopId, floorId, buildingId, deviceId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findWaterByPieDiagrams")
    @ApiOperation("耗水统计饼图")
    public AjaxResult findWaterByPieDiagrams(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        List<DateStatistics> statistics = workshopEnergyService.findWaterByPieDiagrams(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findElectricByPieDiagrams")
    @ApiOperation("耗电统计饼图")
    public AjaxResult findElectricByPieDiagrams(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        List<DateStatistics> statistics = workshopEnergyService.findElectricByPieDiagrams(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/findGasByDiagrams")
    @ApiOperation("耗气统计饼图")
    public AjaxResult findGasByDiagrams(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        List<DateStatistics> statistics = workshopEnergyService.findGasByDiagrams(workshopId, floorId, buildingId, range, LocalDate.parse(begin==null ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(end==null? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    /**
     * 查询设备下拉框信息
     */
    @GetMapping("/listsByDeviceComboBox")
    @ApiOperation("查询设备下拉框信息")
    public AjaxResult listsByDeviceComboBox(DeviceDetail device) {
        return AjaxResult.success(workshopEnergyService.listsByDeviceComboBox(device));
    }

    @GetMapping("/listsByDeviceComboBoxCop")
    @ApiOperation("查询设备下拉框信息Cop")
    public AjaxResult listsByDeviceComboBoxCop(DeviceDetail device) {
        return AjaxResult.success(workshopEnergyService.listsByDeviceComboBoxCop(device));
    }


}
